package com.ds;

import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEntry;

import javax.swing.text.DateFormatter;
import javax.swing.tree.TreeNode;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.net.URL;
import java.net.MalformedURLException;

/**
 * Created by IntelliJ IDEA.
 * User: daniel
 * Date: 29/10/2009
 * Time: 14:56:21
 */
public class Episode implements TreeNode {

    private static final DateFormat DATE_FORMAT = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
    private static final Pattern TORRENTZ_SEEDS_REGEXP = Pattern.compile("Seeds: (-?[\\d,]+) ");
//    private static final Pattern MININOVA_LANG_REGEXP = Pattern.compile("Language: <[^>]+> (\\w*)");
//    private static final Pattern MININOVA_SEEDS_REGEXP = Pattern.compile("Ratio: (-?\\d+) seeds");

    private SyndEntry entry;
    private int seedsCount;
    private String language;

    private TVShow show;


    public Episode(SyndEntry entry) {
        this.entry = entry;
        extractDetailsFromDescription();
    }

    private void extractDetailsFromDescription() {
        //TODO define regular expressions for title and description (both filter and notfilter) in the config.xml to generalize code
        //extract seeds
        Matcher m = TORRENTZ_SEEDS_REGEXP.matcher(getDescription());
        if(m.find()){
            String sSeeds = m.group(1);
            sSeeds = sSeeds.replaceAll(",","");
            try {
                this.seedsCount = Integer.parseInt(sSeeds);
            } catch (NumberFormatException e) {
            }
        }

        //extract language
//        language = "";
//        m = MININOVA_LANG_REGEXP.matcher(getDescription());
//        if(m.find()){
//            language = m.group(1);
//        }
    }


    public Date getEpisodeDate() {
        return entry.getPublishedDate();
    }

    public String getTitle() {
        return entry.getTitle();
    }

    public String getDescription() {
        return entry.getDescription().getValue();
    }

    public String getLink() {
        String resultLink = null;

        //first try enclosures
        List enclosures = entry.getEnclosures();
        if( ! enclosures.isEmpty() ){
            SyndEnclosure se = (SyndEnclosure)enclosures.get(0);
            String tmpUrl = se.getUrl();
            if(tmpUrl != null && tmpUrl.length()>0){
                try {
                    new URL(tmpUrl);
                    //its a valid url, choose it
                    resultLink = tmpUrl;
                } catch (MalformedURLException e) {
                    //do nothing, leave the previous url
                }
            }
        }

        //second, try link item
        if(resultLink == null) {
            resultLink = entry.getLink();
        }

        //third, use uri item
        if(resultLink == null) {
            resultLink = entry.getUri();
        }

        return resultLink;
    }

    public int getSeedsCount(){
        return seedsCount;
    }

    public String getLanguage(){
        return this.language;
    }

    public String toString() {
        String sDate = DATE_FORMAT.format(getEpisodeDate());
        return sDate + " : [" + getSeedsCount() + "] " + getTitle();
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Episode episode = (Episode) o;

        if (entry != null ? !entry.equals(episode.entry) : episode.entry != null) return false;

        return true;
    }

    public int hashCode() {
        return (entry != null ? entry.hashCode() : 0);
    }

    public void setShow(TVShow show) {
        this.show = show;
    }

    //TreeNode implementation
    public TreeNode getChildAt(int childIndex) {
        return null;
    }

    public int getChildCount() {
        return 0;
    }

    public TreeNode getParent() {
        return show;
    }

    public int getIndex(TreeNode node) {
        return 0;
    }

    public boolean getAllowsChildren() {
        return false;
    }

    public boolean isLeaf() {
        return true;
    }

    public Enumeration children() {
        return null;
    }
}
